package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.entity.Order;
import com.cy.store.entity.OrderItem;
import com.cy.store.mapper.OrderMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ICartService;
import com.cy.store.service.IOrderService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.OrderNotFoundException;
import com.cy.store.service.ex.UpdateException;
import com.cy.store.vo.CartVo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private ICartService cartService;

    /**
     * 查询订单信息
     *
     * @param oid
     * @param uid
     * @return
     */
    @Override
    public Order findByOid(Integer oid, Integer uid) {
        Order order = orderMapper.findByOid(oid);
        if (order == null) {
            throw new OrderNotFoundException("订单信息不存在");
        } else if (!order.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        return order;
    }

    /**
     * 创建订单
     *
     * @param aid
     * @param uid
     * @param username
     * @param cids
     * @return
     */
    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
        List<CartVo> list = cartService.getVOByCid(uid, cids);
        //计算商品的总价
        Long totalPrice = 0L;
        for (CartVo item : list) {
            totalPrice += item.getPrice() * item.getNum();
        }


        Address address = addressService.findByAid(aid, uid);

        Order order = new Order();
        order.setUid(uid);
        order.setRecvName(address.getName());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvPhone(address.getPhone());
        order.setRecvAddress(address.getAddress());

        //支付总价
        order.setStatus(0);
        order.setTotalPrice(totalPrice);

        order.setCreatedTime(new Date());
        order.setModifiedTime(new Date());
        order.setCreatedUser(username);
        order.setModifiedUser(username);
        order.setOrderTime(new Date());

        Integer rows = orderMapper.insertOrder(order);

        if (rows != 1) {
            throw new InsertException("插入表单异常");
        }

        //创建订单详细项的数据
        for (CartVo item : list) {
            // 创建一个订单项数据
            OrderItem orderItem = new OrderItem();

            //在执行完insert后 会自动补全oid
            orderItem.setOid(order.getOid());
            orderItem.setPid(item.getPid());
            orderItem.setTitle(item.getTitle());
            orderItem.setImage(item.getImage());
            orderItem.setPrice(item.getRealPrice());
            orderItem.setNum(item.getNum());

            //补全日志
            orderItem.setCreatedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedTime(new Date());
            orderItem.setModifiedUser(username);

            //插入表单项
            rows = orderMapper.insertOrderItem(orderItem);
            if (rows != 1) {
                throw new InsertException("插入订单项数据异常");
            }
        }

        return order;
    }

    /**
     * 支付后更改订单状态
     *
     * @param oid
     * @param username
     * @param uid
     * @return
     */
    @Override
    public void updateByOid(Integer oid, String username, Integer uid, Integer[] cids) {
        Order order = orderMapper.findByOid(oid);
        if (order == null) {
            throw new OrderNotFoundException("订单数据异常");
        } else if (!order.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer rows = orderMapper.updateByOid(oid, username, new Date(), new Date());
        //在更新完表格之后同时删除购物车的相关商品数据
        cartService.deleteCart(uid, cids);
        if (rows != 1) {
            throw new UpdateException("更新订单数据发生异常");
        }
    }
}
